<?php
/**
 * 频道列表页
 *
 *
 */

defined('InShopNC') or exit('Access Invalid!');
class web_channelsControl extends SystemControl {
	public function __construct() {
		header('Content-type:text/html;charset=utf8');
		parent::__construct();
		if (strtoupper(CHARSET) == 'GBK') {
			$_GET = Language::getGBK($_GET);
			$_POST = Language::getGBK($_POST);
		}
		$this->web_channels = Model('web_channels');
	}
	public function indexOp(){
		$field = '*';
		$order = 'channel_id desc';
		$pageIndex = 0;
		$pageCount = 30;

		if($_GET['search']){		//搜索
			$condition['channel_name'] = array('like','%'.$_GET['search'].'%');
		}
		if($_GET['pageCount']){		//每页显示多少
			$pageCount = $_GET['pageCount'];
		}
		if($_GET['pageIndex']){		//第几页
			$pageIndex = $pageCount * ($_GET['pageIndex']-1);
		}
		$limit = $pageIndex.','.$pageCount;
		
		$list = $this->web_channels->showData($condition,$field,$order,$limit);
		Tpl::output('pageIndex',$_GET['pageIndex']);
		Tpl::output('pageCount',$pageCount);
		Tpl::output('list',$list);

		echo '<pre>';print_R($list);
	}
	/**
	 * 添加频道
	 */
	public function add_channelOp(){
		$data['channel_name'] = $_POST['channel_name'];
		$data['channel_style'] = $_POST['channel_style'];
		$data['keywords'] = $_POST['keywords'];
		$data['description'] = $_POST['description'];
		$data['channel_show'] = $_POST['channel_show'];

		$result = $this->web_channels->addDataWebCode($data);
		if($result){
			echo '添加成功';
		}else{
			echo '添加失败';
		}
	}
	/**
	 * 编辑频道
	 */
	public function edit_channelOp(){
		if($_POST){
			$condition['channel_id'] = $_POST['channel_id'];
			$data['channel_name'] = $_POST['channel_name'];
			$data['channel_style'] = $_POST['channel_style'];
			$data['keywords'] = $_POST['keywords'];
			$data['description'] = $_POST['description'];
			$data['channel_show'] = $_POST['channel_show'];
			$data['update_time'] = time();
			$result = $this->web_channels->updateData($condition,$data);
			if($result){
				echo '修改成功';
			}else{
				echo '修改失败';
			}
		}else{
			if($_GET['channel_id'] < 0) return ;
			$condition['channel_id'] = $_GET['channel_id'];
			$list = $this->web_channels->showOneData($condition);
			Tpl::output('list',$list);
		}
	}
	/*******频道结束********/

	/**
	 * 添加新模块
	 */
	public function web_addOp(){
		$model_web_config = Model('web_config');
		$web_array['web_name'] = $_POST["web_name"];			
		$web_array['web_page'] = $_POST["web_page"];	//index首页、channel_tp顶部、channel_fl中部
		$web_array['web_show'] = intval($_POST["web_show"]);
		$web_array['update_time'] = time();
		$web_array['web_sort'] = 255;
		$result = $this->web_channels->addDataWeb($web_array);
		if($result){
			echo '添加成功';
		}else{
			echo '添加失败';
		}
	}
	/**
	 * 模块--基本设置
	 */
	public function web_editOp(){
		$web_id = intval($_GET["web_id"]);
		if (chksubmit()){
			$web_array = array();
			$web_id = intval($_POST["web_id"]);
			$web_array['web_name'] = $_POST["web_name"];
			$web_array['web_show'] = intval($_POST["web_show"]);
			$web_array['update_time'] = time();
			$this->web_channels->updateWeb(array('web_id'=>$web_id),$web_array);
			$this->web_channels->updateWebHtml($web_id,$web_array['style_name']);//更新前台显示的html内容
			$this->log(l('web_config_code_edit').'['.$_POST["web_name"].']',1);
			showMessage(Language::get('nc_common_save_succ'),'index.php?act=web_config&op=web_config');
		}
		$web_list = $this->web_channels->getWebList(array('web_id'=>$web_id));
		Tpl::output('web_array',$web_list[0]);
		Tpl::showpage('web_config.edit');
	}
	/**
	 * 获取商品分类列表
	 */
	public function get_goods_class(){
		$goods_class_arr = Model('goods_class')->getTreeClassList(2);	//二级商品分类//getGoodsClassListAll();	//所有商品分类信息
		foreach ($goods_class_arr as $key => $value) {
			if($value['gc_parent_id'] == 0){
				$goods_arr = array();
				foreach ($goods_class_arr as $k => $v) {
					if($v['gc_parent_id'] == $value['gc_id']){
						$goods_arr[] = array(
							'gc_id' => $v['gc_id'],
							'gc_name' => $v['gc_name'],
						);
					}
				}
				$goods_class[] = array(
					'gc_id' => $value['gc_id'],
					'gc_name' => $value['gc_name'],
					'child' => $goods_arr,
				);
			}
		}
		Tpl::output('goods_class',$goods_class);	//商品分类
	}
	/**********顶部 S***********/
	/**
	 * 顶部模块设计
	 */
	public function edit_channel_tpOp(){
		$condition['web_id'] = $_GET['web_id'];
		$list = $this->web_channels->getCodeList($condition);

		foreach ($list as $key => $value) {
			$var_name = $value['var_name'];
			$arr[$var_name] = unserialize($value['code_info']);		//反序列化
		}
		$this->get_goods_class();	//商品分类
		Tpl::output('arr',$arr);
	}
	/**
	 * ajax 获取商品分类 返回 html
	 */
	public function get_category_listOp(){
		$id = $_GET['id'] ? $_GET['id'] : 0;
		// if(!$id) return ;
		$goods_classObj = Model('goods_class');
		$parent = $goods_classObj->getGoodsClassInfoById($id);

		/*分类下的子信息*/
		$condition['gc_parent_id'] = $id;
		$field = 'gc_id,gc_name,gc_parent_id';
		$list = $goods_classObj->getGoodsClassList($condition,$field);

		$html = '<dl select_class_id="'.$parent["gc_id"].'">
		<dt ondblclick="del_gc_parent('.$parent["gc_id"].');"> <span select_id="'.$parent["gc_id"].'" class="pic"> </span>
		<h4>'.$parent["gc_name"].'</h4>
		<a class="ncap-btn-mini upload-pic" title="上传分类图片" href="javascript:void(0);">
		    <i id="form_'.$parent["gc_id"].'" class="fa fa-upload"></i></a>
		<a class="ncap-btn-mini del" title="删除该分类块" onclick="del_gc_parent('.$parent["gc_id"].');" href="javascript:void(0);"><i class="fa fa-trash"></i></a> </dt>
		<dd>
		<input name="channel_category['.$parent["gc_id"].'][gc_parent][gc_id]" value="'.$parent["gc_id"].'" type="hidden">
		<input name="channel_category['.$parent["gc_id"].'][gc_parent][gc_name]" value="'.$parent["gc_name"].'" type="hidden">
		<input name="channel_category['.$parent["gc_id"].'][gc_parent][gc_pic]" value="" type="hidden"><ul>';
		foreach ($list as $key => $value) {
			$html .= '<li gc_id="'.$value["gc_id"].'" ondblclick="del_goods_class('.$value["gc_id"].');"> <i onclick="del_goods_class('.$value["gc_id"].');"></i>'.$value["gc_name"].'<input name="channel_category['.$value["gc_parent_id"].'][goods_class]['.$value["gc_id"].'][gc_id]" value="'.$value["gc_id"].'" type="hidden">
        <input name="channel_category['.$value["gc_parent_id"].'][goods_class]['.$value["gc_id"].'][gc_name]" value="'.$value["gc_name"].'" type="hidden"></li>';
		}
    	$html .='</ul></dd></dl>';
    	echo $html;
	}
	/**
	 * 保存顶部信息 设置
	 */
	public function code_updateOp() {
		$code_id = intval($_POST['code_id']);
		$web_id = intval($_POST['web_id']);
		$code = $this->web_channels->getCodeRow($code_id,$web_id);
		if (!empty($code)) {
			$code_type = $code['code_type'];
			$var_name = $code['var_name'];
			$code_info = $_POST[$var_name];
			$code_info = $this->web_channels->get_str($code_info,$code_type);
			$state = $this->web_channels->updateCode(array('code_id'=> $code_id),array('code_info'=> $code_info));
		}
		if($state) {
			echo '1';exit;
		} else {
			echo '0';exit;
		}
	}
	/**
	 * 保存选中的大图
	 */
	public function channel_slideOp(){
		$code_id = intval($_POST['code_id']);
		$web_id = intval($_POST['web_id']);
		$code = $this->web_channels->getCodeRow($code_id,$web_id);
		if (!empty($code)) {
			$code_type = $code['code_type'];
			$var_name = $code['var_name'];
			$code_info = $_POST[$var_name];

			$pic_id = intval($_POST['slide_id']);
			if ($pic_id > 0) {
    			$var_name = "slide_pic";
    			$pic_info = $_POST[$var_name];
    			$pic_info['pic_id'] = $pic_id;
    			if (!empty($code_info[$pic_id]['pic_img'])) {//原图片
    			    $pic_info['pic_img'] = $code_info[$pic_id]['pic_img'];
    			}
    			$file_name = 'slide-'.$web_id.'-'.$code_id.'-'.$pic_id;
    			$pic_name = $this->_upload_pic($file_name);//上传图片
    			if (!empty($pic_name)) {
    				$pic_info['pic_img'] = $pic_name;
    			}

			    $code_info[$pic_id] = $pic_info;
			    Tpl::output('pic',$pic_info);
			}
			$code_info = $this->web_channels->get_str($code_info,$code_type);
			$this->web_channels->updateCode(array('code_id'=> $code_id),array('code_info'=> $code_info));

    		Tpl::showpage('web_upload_screen','null_layout');
		}
	}
	/**
	 * 保存焦点区切换小图
	 */	
	public function channel_advOp(){
		$code_id = intval($_POST['code_id']);
		$web_id = intval($_POST['web_id']);
		$code = $this->web_channels->getCodeRow($code_id,$web_id);
		if (!empty($code)) {
			$code_type = $code['code_type'];
			$var_name = $code['var_name'];
			$code_info = $_POST[$var_name];

			$slide_id = intval($_POST['slide_id']);
			$pic_id = intval($_POST['pic_id']);
			if ($pic_id > 0) {		//修改的广告
    			$var_name = "focus_pic";
    			$pic_info = $_POST[$var_name];
    			$pic_info['pic_id'] = $pic_id;
    			if (!empty($code_info[$slide_id]['pic_list'][$pic_id]['pic_img'])) {//原图片
    			    $pic_info['pic_img'] = $code_info[$slide_id]['pic_list'][$pic_id]['pic_img'];
    			}
    			$file_name = 'slide-'.$web_id.'-'.$code_id.'-'.$slide_id.'-'.$pic_id;
    			$pic_name = $this->_upload_pic($file_name);//上传图片
    			if (!empty($pic_name)) {
    				$pic_info['pic_img'] = $pic_name;
    			}
			    $code_info[$slide_id]['pic_list'][$pic_id] = $pic_info;
			    Tpl::output('pic',$pic_info);
			}
			$code_info = $this->web_channels->get_str($code_info,$code_type);
			$this->web_channels->updateCode(array('code_id'=> $code_id),array('code_info'=> $code_info));

    		Tpl::showpage('web_upload_focus','null_layout');
		}
	}

	/**
	 * 更新html内容
	 */
	public function html_updateOp() {
		$web_id = intval($_GET["web_id"]);
		$web_list = $this->web_channels->getWebList(array('web_id'=> $web_id));
		$web_array = $web_list[0];
		if(!empty($web_array) && is_array($web_array)) {
			$this->web_channels->updateWebHtml($web_id);
			showMessage(Language::get('nc_common_op_succ'));
		} else {
			showMessage(Language::get('nc_common_op_fail'));
		}
	}
	/**
	 * 上传图片
	 */
	private function _upload_pic($file_name) {
	    $pic_name = '';
	    if (!empty($file_name)) {
			if (!empty($_FILES['pic']['name'])) {//上传图片
				$upload = new UploadFile();
				$filename_tmparr = explode('.', $_FILES['pic']['name']);
				$ext = end($filename_tmparr);
    			$upload->set('default_dir',ATTACH_EDITOR);
    			$upload->set('file_name',$file_name.".".$ext);
				$result = $upload->upfile('pic');
				if ($result) {
					$pic_name = ATTACH_EDITOR."/".$upload->file_name.'?'.mt_rand(100,999);//加随机数防止浏览器缓存图片
				}
			}
	    }
	    return $pic_name;
	}
	/**********顶部 E***********/


	/**********中部 S***********/
	public function edit_channel_flOp(){
		$condition['web_id'] = $_GET['web_id'];
		$list = $this->web_channels->getCodeList($condition);
		foreach ($list as $key => $value) {
			$var_name = $value['var_name'];
			$arr[$var_name] = unserialize($value['code_info']);		//反序列化
		}
		// echo '<pre>';print_R($arr);
	}
	/**
	 * 保存图片
	 */
	public function upload_picOp() {
		$code_id = intval($_POST['code_id']);
		$web_id = intval($_POST['web_id']);
		$code = $this->web_channels->getCodeRow($code_id,$web_id);
		if (!empty($code)) {
			$code_type = $code['code_type'];
			$var_name = $code['var_name'];
			$code_info = $_POST[$var_name];

			$file_name = 'slide-'.$web_id.'-'.$code_id;
			$pic_name = $this->_upload_pic($file_name);//上传图片
			if (!empty($pic_name)) {
				$code_info['pic'] = $pic_name;
			}

			Tpl::output('var_name',$var_name);
			Tpl::output('pic',$code_info['pic']);
			Tpl::output('type',$code_info['type']);
			// Tpl::output('ap_id',$code_info['ap_id']);
			$code_info = $this->web_channels->get_str($code_info,$code_type);
			$state = $this->web_channels->updateCode(array('code_id'=> $code_id),array('code_info'=> $code_info));
			Tpl::showpage('web_upload_pic','null_layout');
		}
	}

	/**********中部 E***********/

	/**
	 * 商品分类
	 */
	public function category_listOp() {
		$model_class = Model('goods_class');
		$gc_parent_id = intval($_GET['id']);
		$goods_class = $model_class->getGoodsClassListByParentId($gc_parent_id);
		Tpl::output('goods_class',$goods_class);
		Tpl::showpage('web_goods_class','null_layout');
	}

	/**
	 * 商品推荐
	 */
	public function recommend_listOp() {
		$model_web_config = Model('web_config');
		$condition = array();
		$gc_id = intval($_GET['id']);
		if ($gc_id > 0) {
			$condition['gc_id'] = $gc_id;
		}
		$goods_name = trim($_GET['goods_name']);
		if (!empty($goods_name)) {
			$condition['goods_name'] = array('like','%'.$goods_name.'%');
		}
		$goods_list = $model_web_config->getGoodsList($condition,'goods_id desc',6);
		Tpl::output('show_page',$model_web_config->showpage(2));
		Tpl::output('goods_list',$goods_list);
		Tpl::showpage('web_goods.list','null_layout');
	}

	/**
	 * 商品排序查询
	 */
	public function goods_listOp() {
		$model_web_config = Model('web_config');
		$condition = array();
		$order = 'goods_salenum desc,goods_id desc';//销售数量
		$goods_order = trim($_GET['goods_order']);
		if (!empty($goods_order)) {
			$order = $goods_order.' desc,goods_id desc';
		}
		$gc_id = intval($_GET['id']);
		if ($gc_id > 0) {
			$condition['gc_id'] = $gc_id;
		}
		$goods_name = trim($_GET['goods_name']);
		if (!empty($goods_name)) {
			$condition['goods_name'] = array('like','%'.$goods_name.'%');
		}
		$goods_list = $model_web_config->getGoodsList($condition,$order,6);
		Tpl::output('show_page',$model_web_config->showpage(2));
		Tpl::output('goods_list',$goods_list);
		Tpl::showpage('web_goods_order','null_layout');
	}


	

	

	/**
	 * 中部推荐图片
	 */
	public function recommend_picOp() {
		$code_id = intval($_POST['code_id']);
		$web_id = intval($_POST['web_id']);
		$model_web_config = Model('web_config');
		$code = $model_web_config->getCodeRow($code_id,$web_id);
		$key_id = intval($_POST['key_id']);
		$pic_id = intval($_POST['pic_id']);
		if (!empty($code) && $key_id > 0 && $pic_id > 1) {
			$code_info = $code['code_info'];
			$code_type = $code['code_type'];
			$code_info = $model_web_config->get_array($code_info,$code_type);//原数组

			$var_name = "pic_list";
			$pic_info = $_POST[$var_name];
			$pic_info['pic_id'] = $pic_id;
			if (!empty($code_info[$key_id]['pic_list'][$pic_id]['pic_img'])) {//原图片
			    $pic_info['pic_img'] = $code_info[$key_id]['pic_list'][$pic_id]['pic_img'];
			}

			$file_name = 'web-'.$web_id.'-'.$code_id.'-'.$key_id.'-'.$pic_id;
			$pic_name = $this->_upload_pic($file_name);//上传图片
			if (!empty($pic_name)) {
				$pic_info['pic_img'] = $pic_name;
			}

			$recommend_list = array();
			$recommend_list = $_POST['recommend_list'];
			$recommend_list['pic_list'] = $code_info[$key_id]['pic_list'];
			$code_info[$key_id] = $recommend_list;
			$code_info[$key_id]['pic_list'][$pic_id] = $pic_info;

			Tpl::output('pic',$pic_info);
			$code_info = $model_web_config->get_str($code_info,$code_type);
			$state = $model_web_config->updateCode(array('code_id'=> $code_id),array('code_info'=> $code_info));
			Tpl::showpage('web_recommend_pic','null_layout');
		}
	}

	/**
	 * 保存切换图片
	 */
	public function slide_advOp() {
		$code_id = intval($_POST['code_id']);
		$web_id = intval($_POST['web_id']);
		$model_web_config = Model('web_config');
		$code = $model_web_config->getCodeRow($code_id,$web_id);
		if (!empty($code)) {
			$code_type = $code['code_type'];
			$var_name = $code['var_name'];
			$code_info = $_POST[$var_name];

			$pic_id = intval($_POST['slide_id']);
			if ($pic_id > 0) {
    			$var_name = "slide_pic";
    			$pic_info = $_POST[$var_name];
    			$pic_info['pic_id'] = $pic_id;
    			if (!empty($code_info[$pic_id]['pic_img'])) {//原图片
    			    $pic_info['pic_img'] = $code_info[$pic_id]['pic_img'];
    			}
    			$file_name = 'web-'.$web_id.'-'.$code_id.'-'.$pic_id;
    			$pic_name = $this->_upload_pic($file_name);//上传图片
    			if (!empty($pic_name)) {
    				$pic_info['pic_img'] = $pic_name;
    			}

			    $code_info[$pic_id] = $pic_info;
			    Tpl::output('pic',$pic_info);
			}
			$code_info = $model_web_config->get_str($code_info,$code_type);
			$model_web_config->updateCode(array('code_id'=> $code_id),array('code_info'=> $code_info));

    		Tpl::showpage('web_upload_slide','null_layout');
		}
	}
	
	/*********频道模块设计编辑 S*********/
	/**
	 * 编辑页面
	 */
	public function set_channelOp(){
		$condition['channel_id'] = $_GET['channel_id'];
		$list = $this->web_channels->showOneData($condition);

		$list_tp = $this->web_channels->getWebList(array('web_page'=>'channel_tp'));	//顶部模块设计
		$c['web_page'] = 'channel_fl';
		$c['web_id'] = array('in',$list['floor_ids']);
		$list_fl = $this->web_channels->getWebList($c);		//已经选中的中部模块列表
		
		Tpl::output('list_fl',$list_fl);
		Tpl::output('list_tp',$list_tp);
		Tpl::output('list',$list);
		$this->get_goods_class();	//商品分类
	}
	/**
	 * 保存频道模块设计数据
	 */
	public function save_channelOp(){
		$condition['channel_id'] = $_POST['channel_id'];
		$data['gc_id'] = $_POST['gc_id'];
		$data['top_id'] = $_POST['top_id'];
		$data['floor_ids'] = implode(',', $_POST['floor_ids']);
		$data['update_time'] = time();
		$result = $this->web_channels->updateData($condition,$data);
		if($result){
			echo '修改成功';
		}else{
			echo '修改失败';
		}
	}

	/**
	 * ajax 获取中部模块列表
	 * name  	搜索名称
	 * curpage 	选中的页码
	 */
	public function get_channel_flOp(){
		$condition['web_name'] = array('like',"%{$_GET['name']}%");
		$condition['web_page'] = 'channel_fl';
		$list = $this->web_channels->getWebList($condition,1,$limit);
		foreach ($list as $key => $value) {
			$li_html .= '<li><i class="fa fa-list-alt"></i>模块：'.$value['web_name'].'<a href="###" class="ncap-btn-mini ncap-btn-green" onclick="select_floor('.$value['web_id'].');" f_id="'.$value['web_id'].'" f_name="'.$value['web_name'].'"><i class="fa fa-plus-square-o"></i>添加</a><span class="ok"><i class="fa fa-check-circle-o"></i>已添加</span></li>';
		}
		$html = '<ul id="show_list" class="floor_list">'.$li_html.'</ul>
		<div id="show_page_floor" class="pagination">'.$this->web_channels->showpage().'</div>
		<div class="clear"></div>
		<script type="text/javascript">
			$("#show_page_floor .demo").ajaxContent({
				target:"#show_floor_list"
			});
			$("#show_list li").each(function(){
			    var f_id = $(this).find("a").attr("f_id");
			    if($("#floor_"+f_id).size()>0) $(this).addClass("selected");
			});
		</script>';
		echo $html;
	}

	/*********频道模块编辑 E*********/
	
}
